package com.naiterui.ehp.bs.doctor.repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bp.domain.PatientPersonDoctor;
import com.naiterui.ehp.bp.domain.PatientPersonDoctorPK;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 医患关系实体dao
 *
 * @author wangzehao
 */
@Repository
public interface IPatientPersonDoctorRepository extends BaseJpaRepository<PatientPersonDoctor, PatientPersonDoctorPK>  {

    @Query("FROM PatientPersonDoctor WHERE personPK.doctorId=?1 AND personPK.patientId IN (?2)")
    List<PatientPersonDoctor> getByPatientIds(Long doctorId, Collection<Long> patientId);



    @Query("from PatientPersonDoctor where relationFlag = ?1 and personPK.doctorId=?2")
    List<PatientPersonDoctor> getPatientIdsByDoctorId(int relationFlag, Long doctorId);

    @Query(value = "from PatientPersonDoctor where relationFlag = 0 and  personPK.doctorId=?1 and createAt >= ?2 order by createAt desc",
     countQuery = "select count(t) from PatientPersonDoctor t where relationFlag = 0 and  personPK.doctorId=?1 and createAt >= ?2")
    Page<PatientPersonDoctor> getByPeriods(Long doctorId, Date startTime, Pageable pageable) ;

    @Query("from PatientPersonDoctor where personPK.patientId=?1")
    List<PatientPersonDoctor> getDoctorIdsByPatientId(Long patientId);

    /**
     * 获取医生患者总数
     *
     * @param doctorId
     *
     * @return
     */
    @Query("select count(t) from PatientPersonDoctor t where relationFlag = 0 and personPK.doctorId = ?1 " )
     Integer getTotalPatientCount(Long doctorId) ;

    /**
     * 初始化医生今日患者数
     *
     * @param doctorId
     * @param date
     *
     * @return
     */
    @Query( "select count(t) from PatientPersonDoctor t where relationFlag = 0 and personPK.doctorId = ?1 and createAt  > ?2 ")
    Long getTodayPatientCount(Long doctorId, Date date) ;

}
